Display mesh in 3D and using color on 2D¶

In [1]:
import pandas as pd
import holoviews as hv

Read in mesh from hgrid.gr3 file¶

In [2]:
from schimpy import schism_mesh

smesh = schism_mesh.read_mesh('../tests/data/m1_hello_schism/hgrid.gr3')
In [3]:
dfelems = pd.DataFrame(smesh.elems,columns=[0,1,2])
dfelems
Out[3]:
0 1 2
0 0 2 1
1 1 4 3
2 2 5 4
3 3 7 6
4 4 8 7
... ... ... ...
4631 2630 2631 2626
4632 2631 2632 2627
4633 2633 2634 2630
4634 2634 2635 2631
4635 2636 2637 2634

4636 rows × 3 columns

replace 'z' with negative values for depth

In [4]:
dfnodes = pd.DataFrame(smesh.nodes, columns=['x','y','z'])
dfnodes['depth'] = -dfnodes.z
dfnodes = dfnodes.drop(columns=['z'])
dfnodes
Out[4]:
x y depth
0 56000.00 -10400.00 -0.500000
1 55831.58 -10400.00 -0.500000
2 56000.00 -10350.00 -0.944444
3 55663.16 -10400.00 -0.500000
4 55831.58 -10348.07 -1.023290
... ... ... ...
2634 55831.58 10348.07 -1.023290
2635 55663.16 10400.00 -0.500000
2636 56000.00 10350.00 -0.944444
2637 55831.58 10400.00 -0.500000
2638 56000.00 10400.00 -0.500000

2639 rows × 3 columns

TriSurface drawn using nodes and elements¶

Adapted from https://anaconda.org/philippjfr/brain/notebook?version=2017.05.04.1924

The code below allows for the simplices already defined by elems to be used instead of doing a Delaunay triangulation (used from scipy as a way to calculate the simplices)

In [5]:
# uncomment and the run install script below if plotly is not available
#!conda install -y -c conda-forge plotly

hv.extension('plotly')

import param

class TriSurface(hv.TriSurface):
    
    simplices = param.Array()

class TriSurfacePlot(hv.plotting.plotly.TriSurfacePlot):

    style_opts = ['cmap', 'plot_edges']

    def get_data(self, element, ranges, style, **kwargs):
        if element.simplices is None:
            return super(TriSurfacePlot, self).get_data(element, ranges, style, **kwargs)
        x, y, z = (element.dimension_values(i) for i in range(3))
        simplices = element.simplices
        return [dict(x=x, y=y, z=z, simplices=simplices)]
    
hv.Store.register({TriSurface: TriSurfacePlot}, 'plotly')

tris = TriSurface(dfnodes, simplices = dfelems.values).opts(width=800, height=800, cmap='blues_r')
tris.opts(plot_edges=False, colorbar=True)
Out[5]:

Save mesh as html to embed in docs

In [6]:
hv.save(tris,'mesh_surface_colored_by_depth.html')

Trimesh to show z values with color¶

Alternate way of displaying the z (depth values) with color

In [7]:
hv.extension('bokeh')
from holoviews.operation import datashader
trimesh = hv.TriMesh((dfelems.values, hv.Points(dfnodes,vdims='depth'))).opts(cmap='fire',node_alpha=0,edge_color='z',filled=True)
img = datashader.rasterize(trimesh).opts(cmap='rainbow4', colorbar=True, tools=['hover'], width=800)
img
Out[7]:
In [8]:
hv.save(img,'mesh_colored_by_depth.html')
In [ ]: